package com.yonyou.cyxdms.sysAuth.service.service.dudu;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.sysAuth.service.dto.BindDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.BindPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.BindMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 手机号和分机号绑定表 服务实现类
 * </p>
 *
 * @author houkaihua
 * @since 2019-11-06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BindServiceImpl extends ServiceImpl<BindMapper, BindPO> implements BindService {
    //日志对象
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    BindMapper bindMapper;

    /**
     * 分页查询对应数据
     *
     * @param page    分页对象
     * @param bindDTO 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyx.information.service.entity.dto.BindDTO>
     * @author houkaihua
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public IPage<BindDTO> selectPageBysql(Page page, BindDTO bindDTO) {
        if (bindDTO == null) {
            bindDTO = new BindDTO();
        }
        BindPO bindpo = bindDTO.transDtoToPo(BindPO.class);

        List<BindPO> list = bindMapper.selectPageBySql(page, bindpo);
        if (CommonUtils.isNullOrEmpty(list)) {
            page.setRecords(new ArrayList<>());
            return page;
        } else {
            List<BindDTO> result = list.stream().map(m -> m.transPoToDto(BindDTO.class)).collect(Collectors.toList());

            page.setRecords(result);
            return page;
        }
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param bindDTO 查询条件
     * @return java.util.List<com.yonyou.cyx.information.service.entity.dto.BindDTO>
     * @author houkaihua
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public List<BindDTO> selectListBySql(BindDTO bindDTO) {
        if (bindDTO == null) {
            bindDTO = new BindDTO();
        }
        BindPO dtoToPo = bindDTO.transDtoToPo(BindPO.class);
        List<BindPO> list = bindMapper.selectListBySql(dtoToPo);
        if (CommonUtils.isNullOrEmpty(list)) {
            return new ArrayList<>();
        } else {
            return list.stream().map(m -> m.transPoToDto(BindDTO.class)).collect(Collectors.toList());
        }
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param id 主键ID
     * @return com.yonyou.cyx.information.service.entity.dto.BindDTO
     * @author houkaihua
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public BindDTO getById(Long id) {
        BindPO bindpo = bindMapper.selectById(id);
        if (bindpo != null) {
            return bindpo.transPoToDto(BindDTO.class);
        } else {
            throw new DALException("查询失败,没有符合条件的数据");
        }
    }

    /**
     * 根据DTO 进行数据新增
     *
     * @param bindDTO 页面DTO
     * @return int
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public int insert(BindDTO bindDTO) {
        //对对象进行赋值操作
        BindPO bindpo = bindDTO.transDtoToPo(BindPO.class);
        //执行插入
        bindpo.setPageNum(1);
        bindpo.setPageSize(10);
        int row = bindMapper.insert(bindpo);
        //返回插入的值
        return row;
    }

    /**
     * 根据DTO 及ID 进行数据更新
     *
     * @param id      主键ID
     * @param bindDTO 页面DTO
     * @return int
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public int update(Long id, BindDTO bindDTO) {
        BindPO bindpo = bindMapper.selectById(id);
        //对对象进行赋值操作
        bindDTO.transDtoToPo(bindpo);

        if (bindpo.getPageNum() == null || bindpo.getPageNum() == 0) {
            bindpo.setPageNum(1);
        }
        if (bindpo.getPageSize() == null || bindpo.getPageSize() == 0) {
            bindpo.setPageSize(10);
        }

        //执行更新
        int row = bindMapper.updateById(bindpo);
        return row;
    }

    /**
     * 根据ID 进行删除数据
     *
     * @param id
     * @return int
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public int deleteById(Long id) {
        int deleteCount = bindMapper.deleteById(id);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount <= 0) {
            throw new DALException("删除失败,没有符合条件的数据");
        } else {
            return deleteCount;
        }
    }

    /**
     * 根据ids 进行删除
     *
     * @param ids
     * @return int
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public int deleteBatchIds(String ids) {
        List<Long> longList = StringUtils.convertStrToArray(ids, ",", Long.class);
        int deleteCount = bindMapper.deleteBatchIds(longList);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount != longList.size()) {
            throw new DALException("删除失败,没有符合条件的数据");
        } else {
            return deleteCount;
        }
    }

    @Override
    public BindPO getByPhoneAndAppId(String caller, String appId) {
        return bindMapper.getByPhoneAndAppId(caller, appId);
    }

    @Override
    public BindPO getByPhone(String caller) {
        return bindMapper.getByPhone(caller);
    }

    @Override
    public BindPO getByUserCode(String userCode, String appId) {
        return bindMapper.getByUserCode(userCode, appId);
    }

//    @Override
//    public Boolean isBind(String mobile) {
//        BindPO bindPO = getByPhone(mobile);
//        return bindPO != null && !StringUtils.isBlank(bindPO.getExtension());
//    }
}
